<?php
/**
 * 定义 Model_Attachments 类
 *
 * @copyright Copyright (c) 2009 iTeam
 * @author sunnycoolboy
 * @package leadtone
 * @version  Attachments.php 2009-03-31
 */
class Model_Attachments
{
    /**
     * 提供商品信息数据库访问服务的对象
     *
     * @var Table_Articles
     */
    var $_tbAttachments;

    /**
     * 构造函数
     *
     * @return Model_Articles
     */
    function Model_Attachments() {
        $this->_tbAttachments =& FLEA::getSingleton('Table_Attachments');
    }

    /**
     * 保存产品信息
     *
     * @param array $article
     *
     * @return boolean
     */
    function saveAttachments($attachments) {
        if (isset($attachments['atid']) && (int)$attachments['atid'] == 0) {
            unset($attachments['atid']);
        }
        return $this->_tbAttachments->save($attachments);
    }
    //附件文章关联
    function updateAttachments($articleid){
        $data = array();
        if($_SESSION['nowPic']){
            $item = array();
            $item['articleid'] = $articleid;
            foreach($_SESSION['nowPic'] as $row){
                $item['atid'] = $row;
                $data[]=$item;
            }
        }
        if($this->_tbAttachments->saveRowset($data))session_unregister('nowPic');
    }

    /**
     * 删除指定的文章信息
     *
     * @param int $articleId
     *
     * @return boolean
     */
    function removeAttachments($atid) {

        $atid = (int)$atid;

        $attachment = $this->_tbAttachments->find($atid);

        if (!$attachment) {
            FLEA::loadClass('Exception_ArticleNotFound');
            __THROW(new Exception_ArticleNotFound($atid));
            return false;
        }
        return $this->_tbAttachments->removeByPkv($atid,false);
    }


    /**
     * 返回 Table_Articles 对象
     *
     * @return Table_Articles
     */
    function & getTable() {
        return $this->_tbAttachments;
    }
}
